function getAsyncUrl(treeId, treeNode) {
	if (treeNode == null) {
		return path+"beshs/projectAction!queryProjectsByBelongCityID.do?date="+new Date();
	}
	if(treeNode.type=="project"){
		return path+"beshs/communityAction!queryCommunitysByBelongs.do?projectUID="+treeNode.id+"&date="+new Date();
	}
	if(treeNode.type=="community"){
		var url = path+"beshs/buildingAction!queryBuildingsByBelongs.do?projectUID="+treeNode.pid+"&communityUID="+treeNode.id+"&date="+new Date();
		return url;
	}
};
function filter(treeId, parentNode, responseData) {
	// gei node tia添加type 属性 project community，building
	if (parentNode == null) {
		var nodes =[];
		$(responseData).each(function (index,element){
			var node = {};
			node.id=element.id;
			node.name=element.projectName;
			node.pid = 0;
			node.type = "project";
			node.isParent = true;
			nodes.push(node);
			node.showIcon=true;
			node.iconOpen = path+"temperature/images/ztree-img/1_open.png";
			node.iconClose = path+"temperature/images/ztree-img/1_close.png";
		});
		return nodes;
	}
	if (parentNode.type=="project") {
		var pid = parentNode.id;
		var nodes =[];
		$(responseData).each(function (index,element){
			var node = {};
			node.id=element.id;
			node.name=element.communityName;
			node.pid = pid;
			node.type = "community";
			node.isParent = true;
			nodes.push(node);
			node.showIcon=true;
			node.iconOpen = path+"temperature/images/ztree-img/1_open.png";
			node.iconClose = path+"temperature/images/ztree-img/1_close.png";
		});
		return nodes;
	}
	if (parentNode.type=="community") {
		var pid = parentNode.id;
		var nodes =[];
		$(responseData).each(function (index,element){
			var node = {};
			node.id=element.id;
			node.name=element.buildingName;
			node.pid = pid;
			node.type = "building";
			node.isParent = false;
			nodes.push(node);
		});
		return nodes;
	}
}
var setting = {
	async : {
		enable : true,
		contentType : "application/x-www-form-urlencoded",
		url : getAsyncUrl,
		dataFilter : filter
	},
	data : {
		impleData : {  
			enable : true,  
			idKey : "id", 
			pIdKey : "pid",	
			rootId : 0
		}  
	},
	callback : {
		onClick : onClick,
		onRightClick : OnRightClick,
	}
};


function onClick(e, treeId, treeNode) {
	var zTree = $.fn.zTree.getZTreeObj("treeDemo");
	zTree.expandNode(treeNode);
	if (treeNode.type == "building") {
		show();
		treeNodeName = treeNode.name;
		var comm =treeNode.getParentNode();
		projectUID = comm.pid;
		communityUID = treeNode.pid;
		buildingUID = treeNode.id;
	}
}

//判断是小区节点就弹出右键菜单
function OnRightClick(event, treeId, treeNode) {
	var zTree = $.fn.zTree.getZTreeObj("treeDemo");
	zTree.expandNode(treeNode);
	if(treeNode.type == "community"){
		if (!treeNode && event.target.tagName.toLowerCase() != "button"
				&& $(event.target).parents("a").length == 0) {
			zTree.cancelSelectedNode();
			showRMenu("root", event);
		} else if (treeNode && !treeNode.noR) {
			zTree.selectNode(treeNode);
			event = event || window.event;
			showRMenu("node", event);
		}
//		treeNodeName = treeNode.name;
		var parentNode =treeNode.getParentNode();
		projectUID = treeNode.pid;
		communityUID = treeNode.id;
		projectNameString = parentNode.name;
		filetreeNodeName = treeNode.name;
		treeNode.isParent = true;
//		alert(projectNameString);
	}
}
var p;
//弹出右键菜单显示的位置，随鼠标移动！
function showRMenu(type,event) {
	p = $("#left").position();
	event = event || window.event;
	var posX = event.clientX;
	var posY = event.clientY;
//	$("#rMenu div").show();
	var rMenus = document.getElementById("rMenu");
	rMenus.style.display = "block";
	rMenus.style.allowTipHover = true;
	$("#rMenu").offset({
		top : p.top + posY,
		left : p.left + posX,
	});
	$("body").bind("mousedown", onBodyMouseDown);
}
//设置点击body右键菜单隐藏
function hideRMenu() {
	if (rMenu)
		rMenu.css({"display" : "none"});
	$("body").unbind("mousedown", onBodyMouseDown);
}
function onBodyMouseDown(event) {
	if (!(event.target.id == "rMenu" || $(event.target).parents("#rMenu").length > 0)) {
		rMenu.css({
			"display" : "none"
		});
	}
}

var zTree, rMenu;
$(document).ready(function() {
	$.fn.zTree.init($("#treeDemo"), setting, null);
	zTree = $.fn.zTree.getZTreeObj("treeDemo");
	rMenu = $("#rMenu");
});






